package com.example.studentalarm.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import com.contrarywind.adapter.WheelAdapter;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;
import com.example.studentalarm.R;
import com.example.studentalarm.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

public class PickerDialog extends Dialog implements View.OnClickListener {
    private TextView tvCancel, tvSure;
    private WheelView wheelView;
    private ItemOnClick itemOnClick;
    private List<String> mOptionsItems = new ArrayList<>();
    private Context mContext;
    private int index;
    public PickerDialog(@NonNull Context context) {
        super(context, R.style.fragment_offline_dialog);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pick_item);
        tvCancel = findViewById(R.id.tvExit);
        tvSure = findViewById(R.id.tvSure);
        wheelView = findViewById(R.id.wheelview);
        tvCancel.setOnClickListener(this);
        tvSure.setOnClickListener(this);
        wheelView.setCyclic(false);
        String[] time = mContext.getResources().getStringArray(R.array.alarm_time);
        for (String minute : time) {
            mOptionsItems.add(minute);
        }
        ItemAdapter wheelAdapter = new ItemAdapter(mOptionsItems);
        wheelView.setAdapter(wheelAdapter);
        wheelView.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                LogUtil.e("提前多少分钟：" + mOptionsItems.get(index));
                PickerDialog.this.index=index;
            }

        });
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        dialogWindow.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);//设置横向全屏
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvExit:
                dismiss();
                break;
            case R.id.tvSure:
                if (itemOnClick != null) {
                    itemOnClick.onClick(mOptionsItems.get(index));
                }
                dismiss();
                break;

        }
    }


    public void setOnItemClick(ItemOnClick itemOnClick) {
        this.itemOnClick = itemOnClick;
    }


    public interface ItemOnClick {
        void onClick(String minute);
    }


    public class ItemAdapter implements WheelAdapter<String> {

        private List<String> allData;

        public ItemAdapter(List<String> allData) {
            this.allData = allData;
        }


        @Override
        public int getItemsCount() {
            return allData.size();
        }

        @Override
        public String getItem(int index) {
            return allData.get(index);
        }


        @Override
        public int indexOf(String o) {
            return allData.indexOf(o);
        }
    }

}
